package login

import (
	loginres "hft/Http/Controller/login/login_res"
	loginutils "hft/Http/Controller/login/login_utils"
	"hft/Http/Models/request"
	"hft/Http/Services/login"
	utils "hft/Http/Utils"
	"hft/global"

	"github.com/gin-gonic/gin"
)

type Login struct{}

func (l *Login) Login(c *gin.Context) {

	var account *request.Login
	var loginType loginres.LoginType
	var token string
	var err error
	c.ShouldBindJSON(&account)

	c.ShouldBindUri(&loginType)

	if account.LoginType == 1 {
		token, err = loginutils.Phone(loginType.Type, account)
	} else {

	}
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(gin.H{"access_token": token}, c)
}

func (l *Login) SendCode(c *gin.Context) {

	var phone *request.SendCode

	c.ShouldBindJSON(&phone)

	err := login.LoginService.SendCode(global.OC_CONFIG.AliyunSms.LoginSend, phone.Phone)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (l *Login) ValidCode(c *gin.Context) {

	var valid *request.ValidCode

	c.ShouldBindJSON(&valid)

	err := login.LoginService.ValidCode(global.OC_CONFIG.AliyunSms.LoginSend, valid.Phone, valid.Code)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (l *Login) LogOut(c *gin.Context) {
	utils.OkWithMessage("登出成功", c)
}

var LoginController = new(Login)
